package com.dofast.bt.mvp.view.activity.basket;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dofast.bt.R;
import com.dofast.bt.base.BaseMvpActivity;
import com.dofast.bt.bean.AddressBean;
import com.dofast.bt.bean.RegionInfo;
import com.dofast.bt.cityview.OptionsPickerView;
import com.dofast.bt.mvp.presenter.basket.AddressPresenter;
import com.dofast.bt.util.PresenterFactory;
import com.dofast.bt.util.PresenterLoder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * username:Created by hsj on 2016/12/28 0028.
 * class：
 * 类描述：地址选择界面
 */

public class AddressActivity extends BaseMvpActivity<AddressPresenter, AddressView> implements AddressView {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_edit)
    TextView tvEdit;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_province)
    TextView tvProvince;
    @BindView(R.id.ll_province)
    LinearLayout llProvince;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.ll_city)
    LinearLayout llCity;
    @BindView(R.id.tv_coutry)
    TextView tvCoutry;
    @BindView(R.id.ll_country)
    LinearLayout llCountry;
    @BindView(R.id.et_detail)
    EditText etDetail;

    OptionsPickerView pvOptions;


    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            presenter.resetData();
        }

    };

    @Override
    protected int getResourcesId() {
        return R.layout.activity_new_address;
    }

    @Override
    protected void initView() {
        initTitle();
        presenter.initData();
        // 选项选择器
        pvOptions = new OptionsPickerView(this);

        // 设置选择的三级单位
        // pwOptions.setLabels("省", "市", "区");
        pvOptions.setTitle("选择城市");

        // 设置默认选中的三级项目
        // 监听确定选择按钮

        pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                // 返回的分别是三个级别的选中位置
//                String tx = item1.get(options1).getPickerViewText() + item2.get(options1).get(option2).getPickerViewText() + item3.get(options1).get(option2).get(options3).getPickerViewText();
                presenter.getAddress(options1, option2, options3);
            }
        });

        llCity.setClickable(false);
        llProvince.setClickable(false);
        llCountry.setClickable(false);

    }

    @Override
    protected void addOnClickListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public Loader<AddressPresenter> onCreateLoader(int id, Bundle args) {
        return new PresenterLoder<>(this, new PresenterFactory<AddressPresenter>() {
            @Override
            public AddressPresenter create() {
                return new AddressPresenter();
            }
        });
    }

    @Override
    public void initTitle() {
        tvTitle.setText("新增地址");
        tvEdit.setVisibility(View.VISIBLE);
        ivBack.setVisibility(View.VISIBLE);
        tvEdit.setText("完成");
    }

    @Override
    public void gotoActivity(Class<?> tClass, boolean isFinish) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public static void gotoAddress(Context context) {
        Intent intent = new Intent(context, AddressActivity.class);
        context.startActivity(intent);
    }


    @Override
    public Handler getHandle() {
        return handler;
    }

    @Override
    public void resetData(ArrayList<RegionInfo> item1, ArrayList<ArrayList<RegionInfo>> item2, ArrayList<ArrayList<ArrayList<RegionInfo>>> item3) {
        System.out.println(System.currentTimeMillis());
        // 三级联动效果
        pvOptions.setPicker(item1, item2, item3, true);
        pvOptions.setCyclic(true, true, true);
        pvOptions.setSelectOptions(0, 0, 0);
        llCountry.setClickable(true);
        llProvince.setClickable(true);
        llCity.setClickable(true);
    }

    @Override
    public void setProvince(String province) {
        tvProvince.setText(province);
    }

    @Override
    public void setCity(String city) {
        tvCity.setText(city);
    }

    @Override
    public void setCountry(String country) {
        tvCoutry.setText(country);
    }

    @Override
    public String getName() {
        return etName.getText().toString().trim();
    }

    @Override
    public String getPhone() {
        return etPhone.getText().toString().trim();
    }

    @Override
    public String getAddress() {
        return etDetail.getText().toString().trim();
    }

    @Override
    public String getProvince() {
        return tvProvince.getText().toString().trim();
    }

    @Override
    public String getCity() {
        return tvCity.getText().toString().trim();
    }

    @Override
    public String getCountry() {
        return tvCoutry.getText().toString().trim();
    }

    @Override
    public void gotoEditAddress(AddressBean addressBean) {

//        EditAddressActivity.gotoEditAddressActivity(AddressActivity.this);
        Intent intent = new Intent(EditAddressActivity.ADD_ADDRESS);
        intent.putExtra(EditAddressActivity.ADDRESS, addressBean);
        sendBroadcast(intent);
        finish();
    }

    @OnClick({R.id.tv_edit, R.id.iv_back, R.id.ll_province, R.id.ll_city, R.id.ll_country})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_edit:
                presenter.save();
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_province:
                pvOptions.show();
                break;
            case R.id.ll_city:
                pvOptions.show();
                break;
            case R.id.ll_country:
                pvOptions.show();
                break;
        }
    }
}
